home *** CD-ROM | disk | FTP | other *** search
/ Borland JBuilder 6 / jbuilder6.iso / Taiji Applet Pack v2.7 / ScratchImage / ScratchImage.class (.txt) < prev    next >
Encoding:
Java Class File  |  2001-11-06  |  10.0 KB  |  434 lines

  1. import java.applet.Applet;
  2. import java.applet.AudioClip;
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Cursor;
  6. import java.awt.Font;
  7. import java.awt.FontMetrics;
  8. import java.awt.Graphics;
  9. import java.awt.Image;
  10. import java.awt.MediaTracker;
  11. import java.awt.event.InputEvent;
  12. import java.awt.event.MouseEvent;
  13. import java.awt.event.MouseListener;
  14. import java.awt.event.MouseMotionListener;
  15. import java.net.MalformedURLException;
  16. import java.net.URL;
  17. import java.net.URLEncoder;
  18.  
  19. public class ScratchImage extends Applet implements Runnable, MouseListener, MouseMotionListener {
  20.    boolean loaded;
  21.    Thread thread;
  22.    Image image;
  23.    Image buffer;
  24.    Image textim;
  25.    // $FF: renamed from: b java.awt.Graphics
  26.    Graphics field_0;
  27.    // $FF: renamed from: t java.awt.Graphics
  28.    Graphics field_1;
  29.    Color backColor;
  30.    Color color;
  31.    // $FF: renamed from: wi int
  32.    int field_2;
  33.    // $FF: renamed from: he int
  34.    int field_3;
  35.    int wiIm;
  36.    int heIm;
  37.    int size;
  38.    int pause;
  39.    private String statusBarText;
  40.    private AudioClip sound;
  41.    private AudioClip clicSound;
  42.    private AudioClip enterSound;
  43.    private AudioClip scratchSound;
  44.    private boolean scratched;
  45.    private boolean loopSound;
  46.    // $FF: renamed from: fm java.awt.FontMetrics
  47.    private FontMetrics field_4;
  48.    private String enterText;
  49.    private boolean isEnterTextEnabled;
  50.    private int wiEnterText;
  51.    private int enterTextHeight;
  52.    private Color enterTextColor;
  53.    private int decx;
  54.    private int decy;
  55.  
  56.    public String getAppletInfo() {
  57.       return "Name: ScratchImage\r\nAuthor: Taiji Software\r\n";
  58.    }
  59.  
  60.    public ScratchImage() {
  61.       this.backColor = Color.black;
  62.       this.pause = 5;
  63.       this.color = new Color(181, 181, 181);
  64.       this.size = 4;
  65.       ((Component)this).addMouseListener(this);
  66.       ((Component)this).addMouseMotionListener(this);
  67.    }
  68.  
  69.    public void register() {
  70.       try {
  71.          URL u = new URL("http://www.taijisoftware.com");
  72.          ((Applet)this).getAppletContext().showDocument(u, "_blank");
  73.          this.stop();
  74.       } catch (Exception e) {
  75.          System.out.println(e);
  76.          this.stop();
  77.       }
  78.    }
  79.  
  80.    public void init() {
  81.       String codeBase = null;
  82.  
  83.       try {
  84.          codeBase = (new URL(((Applet)this).getCodeBase().toString())).getHost();
  85.          System.out.println("Copyright 1999, 2001 Taiji Software(tm)\nYour domain name is : " + codeBase);
  86.          codeBase = codeBase.toUpperCase();
  87.       } catch (Exception var10) {
  88.       }
  89.  
  90.       if (!((Applet)this).getCodeBase().toString().toUpperCase().startsWith("FILE") || ((Applet)this).getParameter("debug") != null) {
  91.          String regCode = ((Applet)this).getParameter("registration_code");
  92.          if (regCode == null) {
  93.             regCode = ((Applet)this).getParameter("reg_domain");
  94.             if (regCode == null) {
  95.                this.register();
  96.             } else {
  97.                if (regCode.length() == codeBase.length() + 4 && !codeBase.startsWith("WWW.")) {
  98.                   codeBase = "WWW." + codeBase;
  99.                } else if (regCode.length() == codeBase.length() - 4 && codeBase.startsWith("WWW.")) {
  100.                   codeBase = codeBase.substring(4);
  101.                }
  102.  
  103.                char[] chars = new char[codeBase.length()];
  104.                codeBase.getChars(0, codeBase.length(), chars, 0);
  105.                String key = new String("haricot");
  106.                char[] chars2 = new char[key.length()];
  107.                key.getChars(0, key.length(), chars2, 0);
  108.  
  109.                for(int i = 0; i < codeBase.length(); ++i) {
  110.                   int j;
  111.                   if (i >= key.length()) {
  112.                      j = i - key.length() * (i / key.length());
  113.                   } else {
  114.                      j = i;
  115.                   }
  116.  
  117.                   chars[i] += chars2[j];
  118.                   chars[i] = (char)(chars[i] - chars[i] / 26 * 26 + 97);
  119.                }
  120.  
  121.                String res = new String(chars);
  122.                if (!res.equalsIgnoreCase(regCode)) {
  123.                   this.register();
  124.                }
  125.             }
  126.          } else if (!regCode.equals("settevercsedegnamiaj") && !regCode.equals("8078")) {
  127.             this.register();
  128.          }
  129.       }
  130.  
  131.       this.getParameters();
  132.  
  133.       int n;
  134.       for(n = 0; ((Applet)this).getParameter("image" + Integer.toString(n + 1)) != null; ++n) {
  135.       }
  136.  
  137.       if (n > 1) {
  138.          n = (int)((double)n * Math.random()) + 1;
  139.       }
  140.  
  141.       this.image = ((Applet)this).getImage(((Applet)this).getCodeBase(), ((Applet)this).getParameter("image" + Integer.toString(n)));
  142.       MediaTracker tracker = new MediaTracker(this);
  143.       tracker.addImage(this.image, 0);
  144.  
  145.       try {
  146.          tracker.waitForAll();
  147.          this.loaded = !tracker.isErrorAny();
  148.       } catch (Exception e) {
  149.          System.err.println(e);
  150.       }
  151.  
  152.       if (!this.loaded) {
  153.          this.stop();
  154.       }
  155.  
  156.       this.wiIm = this.image.getWidth(this);
  157.       this.heIm = this.image.getHeight(this);
  158.       this.decx = (this.field_2 - this.wiIm) / 2;
  159.       this.decy = (this.field_3 - this.heIm) / 2;
  160.       this.buffer = ((Component)this).createImage(this.field_2, this.field_3);
  161.       this.field_0 = this.buffer.getGraphics();
  162.       this.textim = ((Component)this).createImage(this.field_2, this.field_3);
  163.       this.field_1 = this.textim.getGraphics();
  164.       this.field_0.setColor(this.enterTextColor);
  165.       String fontName = ((Applet)this).getParameter("enter_text_font");
  166.       if (fontName == null) {
  167.          fontName = "TimesRoman";
  168.       }
  169.  
  170.       int style = 1;
  171.       String s = ((Applet)this).getParameter("enter_text_style");
  172.       if (s != null) {
  173.          if (s.equals("bold")) {
  174.             style = 1;
  175.          } else if (s.equals("italic")) {
  176.             style = 2;
  177.          } else if (s.equals("bold_italic")) {
  178.             style = 3;
  179.          } else {
  180.             style = 0;
  181.          }
  182.       }
  183.  
  184.       this.field_1.setFont(new Font(fontName, style, this.enterTextHeight));
  185.       this.field_4 = this.field_1.getFontMetrics();
  186.       if (this.enterText != null) {
  187.          this.wiEnterText = this.field_4.stringWidth(this.enterText);
  188.       }
  189.  
  190.       if (this.sound != null) {
  191.          if (this.loopSound) {
  192.             this.sound.loop();
  193.             return;
  194.          }
  195.  
  196.          this.sound.play();
  197.       }
  198.  
  199.    }
  200.  
  201.    public void getParameters() {
  202.       this.field_2 = ((Component)this).getSize().width;
  203.       this.field_3 = ((Component)this).getSize().height;
  204.       String s = ((Applet)this).getParameter("pause");
  205.       if (s != null) {
  206.          this.pause = Integer.parseInt(s);
  207.       }
  208.  
  209.       s = ((Applet)this).getParameter("size");
  210.       if (s != null) {
  211.          this.size = Integer.parseInt(s);
  212.          this.size = (int)((double)this.size / (double)2.0F) - 1;
  213.       }
  214.  
  215.       if (this.getColor("color") != null) {
  216.          this.color = this.getColor("color");
  217.       }
  218.  
  219.       ((Component)this).setBackground(this.getColor("background_color"));
  220.       this.statusBarText = ((Applet)this).getParameter("status_bar_text");
  221.       s = ((Applet)this).getParameter("sound_name");
  222.       if (s != null) {
  223.          this.sound = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), s);
  224.       }
  225.  
  226.       s = ((Applet)this).getParameter("loop_sound");
  227.       if (s != null) {
  228.          if (s.equals("yes")) {
  229.             this.loopSound = true;
  230.          } else {
  231.             this.loopSound = false;
  232.          }
  233.       }
  234.  
  235.       s = ((Applet)this).getParameter("clic_sound_name");
  236.       if (s != null) {
  237.          this.clicSound = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), s);
  238.       }
  239.  
  240.       s = ((Applet)this).getParameter("enter_sound_name");
  241.       if (s != null) {
  242.          this.enterSound = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), s);
  243.       }
  244.  
  245.       s = ((Applet)this).getParameter("scratch_sound_name");
  246.       if (s != null) {
  247.          this.scratchSound = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), s);
  248.       }
  249.  
  250.       this.enterText = ((Applet)this).getParameter("enter_text");
  251.       s = ((Applet)this).getParameter("enter_text_height");
  252.       if (s != null) {
  253.          this.enterTextHeight = Integer.parseInt(s);
  254.       }
  255.  
  256.       this.enterTextColor = this.getColor("enter_text_color");
  257.       if (this.enterTextColor == null) {
  258.          this.enterTextColor = Color.white;
  259.       }
  260.  
  261.    }
  262.  
  263.    public Color getColor(String param) {
  264.       String s = ((Applet)this).getParameter(param);
  265.       if (s != null) {
  266.          if (s.substring(0, 1).equals("#")) {
  267.             s = s.substring(1);
  268.             int i = Integer.parseInt(s, 16);
  269.             return new Color(i);
  270.          } else {
  271.             return null;
  272.          }
  273.       } else {
  274.          return null;
  275.       }
  276.    }
  277.  
  278.    public void start() {
  279.       if (this.thread == null) {
  280.          this.thread = new Thread(this);
  281.          this.thread.start();
  282.       }
  283.  
  284.    }
  285.  
  286.    public void stop() {
  287.       if (this.thread != null) {
  288.          this.thread = null;
  289.       }
  290.  
  291.       if (this.sound != null) {
  292.          this.sound.stop();
  293.       }
  294.  
  295.    }
  296.  
  297.    public void run() {
  298.       Thread.currentThread().setPriority(1);
  299.       this.field_0.setColor(this.color);
  300.       this.field_0.clearRect(0, 0, this.field_2, this.field_3);
  301.  
  302.       while(true) {
  303.          ((Component)this).repaint();
  304.  
  305.          try {
  306.             Thread.sleep((long)this.pause);
  307.          } catch (InterruptedException var1) {
  308.          }
  309.       }
  310.    }
  311.  
  312.    public final synchronized void paint(Graphics g) {
  313.       if (this.loaded) {
  314.          if (this.isEnterTextEnabled) {
  315.             this.field_1.drawImage(this.buffer, 0, 0, this);
  316.             this.field_1.setColor(this.enterTextColor);
  317.             this.field_1.drawString(this.enterText, (this.field_2 - this.wiEnterText) / 2, this.field_3 / 2);
  318.             g.drawImage(this.textim, 0, 0, this);
  319.          } else {
  320.             g.drawImage(this.buffer, 0, 0, this);
  321.          }
  322.       }
  323.    }
  324.  
  325.    public final void update(Graphics g) {
  326.       this.paint(g);
  327.    }
  328.  
  329.    public URL giveURL(String url) {
  330.       try {
  331.          if (url.toUpperCase().startsWith("HTTP")) {
  332.             return new URL(url);
  333.          } else if (url.toUpperCase().startsWith("FTP")) {
  334.             int p = url.indexOf(":");
  335.             int p2 = url.indexOf(":", p + 1);
  336.             if (p2 != -1) {
  337.                url = url.substring(0, p + 3) + URLEncoder.encode(url.substring(p + 3, url.length()));
  338.             }
  339.  
  340.             p = url.indexOf("%40");
  341.             if (p != -1) {
  342.                url = url.substring(0, p) + "@" + url.substring(p + 3, url.length());
  343.             }
  344.  
  345.             return new URL(url);
  346.          } else {
  347.             return new URL(((Applet)this).getCodeBase(), url);
  348.          }
  349.       } catch (MalformedURLException e) {
  350.          System.out.println(e);
  351.          return null;
  352.       }
  353.    }
  354.  
  355.    public void mouseClicked(MouseEvent e) {
  356.    }
  357.  
  358.    public void mouseEntered(MouseEvent e) {
  359.       if (this.enterText != null) {
  360.          this.isEnterTextEnabled = true;
  361.          this.field_0.setColor(this.enterTextColor);
  362.          ((Component)this).repaint();
  363.       }
  364.  
  365.       if (this.statusBarText != null) {
  366.          ((Applet)this).showStatus(this.statusBarText);
  367.       }
  368.  
  369.       if (this.enterSound != null) {
  370.          this.enterSound.play();
  371.       }
  372.  
  373.       ((Component)this).setCursor(new Cursor(12));
  374.    }
  375.  
  376.    public void mouseExited(MouseEvent e) {
  377.       if (this.isEnterTextEnabled) {
  378.          this.isEnterTextEnabled = false;
  379.          ((Component)this).repaint();
  380.       }
  381.  
  382.       ((Component)this).setCursor(new Cursor(0));
  383.    }
  384.  
  385.    public void mousePressed(MouseEvent e) {
  386.       if (((InputEvent)e).isMetaDown()) {
  387.          this.field_0.setColor(this.color);
  388.          this.field_0.clearRect(0, 0, this.field_2, this.field_3);
  389.       } else {
  390.          if (this.clicSound != null) {
  391.             this.clicSound.play();
  392.          }
  393.  
  394.       }
  395.    }
  396.  
  397.    public void mouseReleased(MouseEvent e) {
  398.       if (this.scratched && this.scratchSound != null) {
  399.          this.scratchSound.stop();
  400.          this.scratched = false;
  401.       }
  402.  
  403.    }
  404.  
  405.    public void mouseDragged(MouseEvent e) {
  406.       if (!((InputEvent)e).isMetaDown()) {
  407.          int x = e.getX();
  408.          int y = e.getY();
  409.          x -= this.decx;
  410.          y -= this.decy;
  411.  
  412.          for(int i = y - this.size; i <= y + this.size; ++i) {
  413.             for(int j = x - this.size; j <= x + this.size; ++j) {
  414.                try {
  415.                   if (!(j + 1 > this.wiIm | i < 0 | j < 0 | i + 1 > this.heIm)) {
  416.                      this.field_0.drawImage(this.image, this.decx + j, this.decy + i, this.decx + j + 1, this.decy + i + 1, j, i, j + 1, i + 1, this);
  417.                   }
  418.                } catch (Exception var6) {
  419.                }
  420.             }
  421.          }
  422.  
  423.          if (!this.scratched && this.scratchSound != null) {
  424.             this.scratchSound.loop();
  425.             this.scratched = true;
  426.          }
  427.  
  428.       }
  429.    }
  430.  
  431.    public void mouseMoved(MouseEvent e) {
  432.    }
  433. }
  434.